package com.yueya.common.util;

import java.io.FileOutputStream;
import java.io.OutputStream;

import sun.misc.BASE64Decoder;

public class Base64Util {

	// base64字符串转化成图片
	public static String GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
		String[] imgStrs = imgStr.split(",");
		if (imgStr == null || imgStrs.length<2) // 图像数据为空
			return "";
		imgStr = imgStrs[1];
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成jpeg图片
			String fileid = UUIDGenerator.getUUID();
			String imgFilePath = UploadUtil.getImageSavePath() + fileid+".jpg";// 新生成的图片
			OutputStream out = new FileOutputStream(UploadUtil.getUploadSavePath() + imgFilePath);
			out.write(b);
			out.flush();
			out.close();
			return imgFilePath;
		} catch (Exception e) {
			return "";
		}
	}

}
